package object
/*环境可以将值与名称相关联，这样就可以在环境中用关联的名称来跟踪值,
从本质上讲，环境是一个将字符串与对象相关联的哈希映射*/
type Environment struct{
	store map[string]Object
	outer *Environment
}
/*为什么不直接使用map，而是使用封装?*/
func NewEnvironment() *Environment{
	s := make(map[string]Object)
	return &Environment{store:s,outer: nil}
}

func (e *Environment) Get(name string)(Object,bool){
	obj,ok := e.store[name]
	if !ok && e.outer != nil{
		obj,ok = e.outer.Get(name)
	}
	return obj,ok
}

func (e *Environment) Set(name string,val Object) Object{
	e.store[name] = val
	return val
}

func NewEnclosedEnvironment(outer *Environment) *Environment{
	env := NewEnvironment()
	env.outer = outer
	return env
}